/************************************************************************************/
/*  Program name CAPAPIMP_ENCUM_LINE.sql                                            */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAPIMP_ENCUM_LINE';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_AP_ENCUMBRANCE_LINES;

	lv_Next_Seq_ID	NUMBER;
	lv_SOB_Name		VARCHAR2(20);
	lv_COA_ID		NUMBER;
	lv_Ref2_ID		NUMBER;

BEGIN

	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	AP_ENCUMBRANCE_LINES_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT INTO CRST_AP_ENCUMBRANCE_LINES_MAP VALUES
			(
			C1.ENCUMBRANCE_LINE_ID,
			lv_Next_Seq_ID
			);

			lv_SOB_Name	:=	CAPBRIMP_COMMON.GetSOBShortNameOU(CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME));
			--lv_SOB_Name	:=	CAPBRIMP_COMMON.GetSOBShortName(CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME));
			lv_COA_ID	:=	CAPBRIMP_COMMON.GetCOAID(lv_SOB_Name);
			
			IF	C1.REFERENCE6	=	'AP Invoices'	THEN
				lv_Ref2_ID	:=	CAPBRIMP_MAPPING.GetAPInvoiceIDMapping(TO_NUMBER(C1.REFERENCE2));
			ELSE
				lv_Ref2_ID	:=	NULL;
			END IF;

			INSERT INTO AP_ENCUMBRANCE_LINES_ALL
			VALUES
			(
			lv_Next_Seq_ID,											--ENCUMBRANCE_LINE_ID,
			CAPBRIMP_MAPPING.GetAPDistIDMapping(C1.INVOICE_DISTRIBUTION_ID),	--INVOICE_DISTRIBUTION_ID,
			C1.ENCUMBRANCE_LINE_TYPE,
			C1.ACCOUNTING_DATE,
			C1.PERIOD_NAME,
			C1.REVERSAL_FLAG,
			C1.ENCUMBRANCE_TYPE_ID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.CCID_D),		--CODE_COMBINATION_ID,
			C1.ACCOUNTED_DR,
			C1.ACCOUNTED_CR,
			CAPBRIMP_MAPPING.GetAEHeaderIDMapping(C1.AE_HEADER_ID),	--AE_HEADER_ID,
			C1.GL_SL_LINK_ID,
			CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME),					--ORG_ID,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),			--CREATED_BY,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),		--LAST_UPDATED_BY,
			C1.LAST_UPDATE_LOGIN,
			C1.REFERENCE1,
			TO_CHAR(lv_Ref2_ID),									--REFERENCE2,
			C1.REFERENCE3,
			C1.REFERENCE4,
			C1.REFERENCE5,
			C1.REFERENCE6,
			TO_CHAR(CAPBRIMP_COMMON.GetSOBID(lv_SOB_Name)),			--REFERENCE7,
			C1.REFERENCE8,
			C1.REFERENCE9,
			C1.REFERENCE10,
			C1.STAT_AMOUNT,
			C1.SUBLEDGER_DOC_SEQUENCE_ID,
			C1.SUBLEDGER_DOC_SEQUENCE_VALUE,
			C1.PROGRAM_UPDATE_DATE,
			C1.PROGRAM_APPLICATION_ID,
			C1.PROGRAM_ID,
			C1.REQUEST_ID
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/
